package com.example.drug.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.example.common.entity.HDoctorNotice;
import com.example.common.entity.Pharmacist;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.drug.dto.req.NoticeListReq;
import com.example.drug.dto.resp.PharmacistInfo;
import com.example.drug.jpa.HDoctorNoticeRepository;
import com.example.drug.jpa.PharmacistRepository;
import com.example.drug.mapper.PharmacistMapper;
import com.example.drug.service.HomeService;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Resource
    private PharmacistRepository pharmacistRepository;
    @Resource
    private HDoctorNoticeRepository hDoctorNoticeRepository;

    @Resource
    private PharmacistMapper pharmacistMapper;

    @Override
    public HashMap<String, Object> getIndexMsg(long DCID) {
        PharmacistInfo obj = pharmacistMapper.findInfoByDCID(DCID);
        CheckUtils.checkNull(obj, new ApiException(10000, "药师信息有误"));

        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        
        Date beg = null, end = null;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 24);
        
            beg = df.parse(df.format(now));
            end = df.parse(df.format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer CountTodayNo = pharmacistRepository.findCountTodayByDcId(DCID,0,beg,end);
        Integer CountTodayYes = pharmacistRepository.countTodayChecked(DCID,beg,end);

        HDoctorNotice Notice = hDoctorNoticeRepository.findTopOne();

        HashMap<String, Object> res = new HashMap<>();
        res.put("Pharmacist",obj);
        res.put("CountTodayNo",CountTodayNo);
        res.put("CountTodayYes",CountTodayYes);
        res.put("Notice",Notice);
        return res;
    }

    @Override
    public Map<String,Object> getDoctorNotice(NoticeListReq noticeListReq){
        Map<String,Object> result=new HashMap<>();
        long count = hDoctorNoticeRepository.countAll();
        List<HDoctorNotice> list = hDoctorNoticeRepository.findPageOrderCreateTimeDesc((noticeListReq.getPage()-1)* noticeListReq.getLimit(),noticeListReq.getLimit());
        result.put("total",count);
        result.put("list",list);
        return result;
    }

    @Override
    public boolean SaveIsOnline(long DCID){
        Pharmacist obj = pharmacistRepository.findById(DCID).orElse(null);
        obj.setIsOnline(!obj.getIsOnline());
        pharmacistRepository.save(obj);
        return obj.getIsOnline();
    }

    @Override
    public boolean SaveIsExamine(long DCID){
        Pharmacist obj = pharmacistRepository.findById(DCID).orElse(null);
        obj.setIsExamine(!obj.getIsExamine());
        pharmacistRepository.save(obj);
        return obj.getIsExamine();
    }

    @Override
    public PharmacistInfo PharmacistMsg(long DCID){
        PharmacistInfo obj = pharmacistMapper.findInfoByDCID(DCID);
        CheckUtils.checkNull(obj, new ApiException(10000, "药师信息有误"));
        return obj;
    }

    @Override
    public HashMap<String, Object> PharmacistSeting(long DCID){
        Pharmacist obj = pharmacistRepository.findById(DCID).orElse(null);
        CheckUtils.checkNull(obj, new ApiException(10000, "用户不存在"));
        HashMap<String, Object> res = new HashMap<>();
        res.put("IsOnline",obj.getIsOnline());
        res.put("IsExamine",obj.getIsExamine());
        return res;
    }
}
